//
//  MineVc.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/10/29.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "MineVc.h"
// Model
#import "MineViewModel.h"
#import "MineItemModel.h"
// View
#import "MineNaviBar.h"
#import "MineUserHead.h"
#import "MineOrderIMCard.h"
#import "MineToolCard.h"
#import "MineHalfCard.h"
// Controller
#import "PosterViewController.h"
#import "SettingViewController.h"
#import "MessageCenterViewController.h"
#import "UserInfoViewController.h"
#import "YunOrderListController.h"
#import "ShopCartViewController.h"
#import "OrderSegmentViewController.h"
#import "MyTrajectorySegViewController.h"
#import "SubscribeSegViewController.h"
#import "MyCouponSegViewController.h"
#import "CollectSegmentViewController.h"
#import "ScheduleManagerVC.h"
#import "SeatsListViewController.h"
#import "MarriagePlanViewController.h"
#import "WedingRegistrationViewController.h"
#import "MarrySpeechCenterViewController.h"
#import "MarryGameSubViewController.h"
#import "BudgetHomeVC.h"
#import "BudgetPieVC.h"
#import "MyBalanceViewController.h"
/// 我推荐的客户
#import "MyRecommandViewController.h"
/// 推荐官收益
#import "AgentDetailViewController.h"
/// 最新邀请好友
#import "InviteFriendsVc.h"
/// 积分
#import "MyPointsListVc.h"
@interface MineVc ()
@property(nonatomic, strong) MineViewModel *viewModel;
@property(nonatomic, strong) UIScrollView *scrollView;
@property(nonatomic, strong) UIImageView *topBgImageView;
@property(nonatomic, strong) MineNaviBar *naviBar;
@property(nonatomic, strong) MineUserHead *userHead;
@property(nonatomic, strong) MineOrderIMCard *orderImCard;
@property(nonatomic, strong) MineToolCard *toolCard;
@property(nonatomic, strong) MineHalfCard *halfCard;
@end

@implementation MineVc

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.viewModel request:NO];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.viewModel request:YES];
    [AppGlobalCenter.share preRequestMyQr:nil];
}
- (void)makeUI {
    [super makeUI];
    //屏幕常亮
    [UIApplication sharedApplication].idleTimerDisabled = YES;
    self.navigationController.navigationBarHidden = YES;
    #if RT_INTERACTIVE_PUSH
        self.rt_navigationController.rt_enableInteractivePush = YES;
    #endif
    self.view.backgroundColor = rgba(244, 248, 255, 1);
    [self.view addSubview:self.topBgImageView];
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.naviBar];
    [self.scrollView addSubview:self.userHead];
    [self.scrollView addSubview:self.orderImCard];
    [self.scrollView addSubview:self.toolCard];
    [self.scrollView addSubview:self.halfCard];
    
    self.topBgImageView.frame = CGRectMake(0, 0, kScreenWidth, ceil(264 * ADAPTER_WIDTH));
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    CGFloat orinY = kNavBarAndStatusBarHeight + 16;
    
    self.userHead.top = orinY;
    orinY = self.userHead.bottom + 20;
    
    self.orderImCard.top = orinY;
    orinY = self.orderImCard.bottom + 12;
    
    self.halfCard.top = orinY;
    orinY = self.halfCard.bottom + 12;
    
    self.toolCard.top = orinY;
    orinY = self.toolCard.bottom;
    
    self.scrollView.contentSize = CGSizeMake(0, MAX(self.scrollView.height + 1, orinY));
}
- (void)handleEvent {
    [super handleEvent];
    WK(weakSelf)
    [self.naviBar setClickAction:^(MineItemModel * _Nonnull itemModel) {
        [weakSelf handleItemModel:itemModel];
    }];
    [self.userHead setClickAction:^(MineItemModel * _Nonnull itemModel) {
        [weakSelf handleItemModel:itemModel];
    }];
    [self.orderImCard setClickAction:^(MineItemModel * _Nonnull itemModel) {
        [weakSelf handleItemModel:itemModel];
    }];
    [self.toolCard setClickAction:^(MineItemModel * _Nonnull itemModel) {
        [weakSelf handleItemModel:itemModel];
    }];
    [self.halfCard setClickAction:^(MineItemModel * _Nonnull itemModel) {
        [weakSelf handleItemModel:itemModel];
    }];
}
- (void)bindViewModel {
    [super bindViewModel];
    WK(weakSelf)
    [self.viewModel setRefreshCount:^(MineCountModel * _Nonnull countModel) {
        weakSelf.orderImCard.countModel = countModel;
    }];
    [self.viewModel setRefreshUserInfo:^{
        [weakSelf.userHead refresh];
    }];
    [self preCache];
}
- (void)preCache {
    //推荐官图片预加载
    [TT_OSSManager cacheSourceUrl:AgentIntroduceImageUrl1];
    [TT_OSSManager cacheSourceUrl:AgentIntroduceImageUrl2];
    [TT_OSSManager cacheSourceUrl:AgentIntroduceImageUrl3];
}
#pragma mark - 处理事件分发
- (void)handleItemModel:(MineItemModel *)itemModel {
    switch (itemModel.action.integerValue) {
        case ItemActionNaviMessage:
        {
            MessageCenterViewController *vc = [[MessageCenterViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case ItemActionNaviSetting:
        {
            SettingViewController *vc = [[SettingViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case ItemActionNaviQrCode:
        {
            InviteFriendsVc *vc = [[InviteFriendsVc alloc]init];
            //PosterViewController *vc = [[PosterViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case ItemActionUserInfo:
        {
            UserInfoViewController *vc = [[UserInfoViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case ItemActionConsumeOrder:
        {
            YunOrderListController *vc = [[YunOrderListController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case ItemActionBill:
        {
            MyBalanceViewController *vc = [[MyBalanceViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case ItemActionOrder:
        {
            OrderSegmentViewController *vc = [[OrderSegmentViewController alloc]init];
            vc.selectIndex = 0;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case ItemActionIM:
        {
            [[TT_IMContactManager manager] ImContactWithType:TT_IMContactType_kefu];
        }
            break;
        case ItemActionCollect:
        {
            CollectSegmentViewController *vc = [[CollectSegmentViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case ItemActionRedPaper:
        {
            MyCouponSegViewController *vc = [[MyCouponSegViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case ItemActionYuYue:
        {
            SubscribeSegViewController *vc = [[SubscribeSegViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case ItemActionZuJi:
        {
            MyTrajectorySegViewController *vc = [[MyTrajectorySegViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case ItemActionToolHunLiGuanJia:
            [[TT_IMContactManager manager] ImContactWithType:TT_IMContactType_tongchoushi];
            break;
        case ItemActionToolHunQianQingGanZiXun:
            [[TT_IMContactManager manager] ImContactWithType:TT_IMContactType_qingganzixun];
            break;
        case ItemActionToolHuangDaoJiRi:
        {
            ScheduleManagerVC *vc = [[ScheduleManagerVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case ItemActionToolXiWeiAnPaiBiao:
        {
            SeatsListViewController *vc = [[SeatsListViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case ItemActionToolBeiHunJiHua:
        {
            MarriagePlanViewController *vc = [[MarriagePlanViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case ItemActionToolHunLiDengJiChu:
        {
            WedingRegistrationViewController *vc = [[WedingRegistrationViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case ItemActionToolShiYanZhiCiKu:
        {
            MarrySpeechCenterViewController *vc = [[MarrySpeechCenterViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case ItemActionToolJieQinYouXi:
        {
            MarryGameSubViewController *vc = [[MarryGameSubViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case ItemActionToolHunLiYuSuanBiao:
        {
            id obj = [[TTUserModel sharedObject].userModelDefault valueForKey:@"buget_totalMoney"];
            if (obj == nil) {
                BudgetHomeVC *vc = [[BudgetHomeVC alloc]init];
                vc.native = YES;
                [self.navigationController pushViewController:vc animated:YES];
            } else {
                BudgetPieVC *vc = [[BudgetPieVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
            break;
        case ItemActionTuiJianRen:
        {
            AgentDetailViewController *vc = [[AgentDetailViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case ItemActionJiFen:
        {
            MyPointsListVc *vc = [[MyPointsListVc alloc]init];
            vc.countModel = self.viewModel.countModel;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        default:
            break;
    }
}
#pragma mark - lazy
- (MineViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[MineViewModel alloc]init];
        _viewModel.parentVc = self;
    }
    return _viewModel;
}
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        UIScrollView *view = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTabBarHeight)];
        if (@available(iOS 11.0, *)) {
            view.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        view.backgroundColor = [UIColor clearColor];
        view.showsVerticalScrollIndicator = NO;
        _scrollView = view;
    }
    return _scrollView;
}
- (UIImageView *)topBgImageView {
    if (!_topBgImageView) {
        UIImageView *iv = [[UIImageView alloc]init];
        iv.image = [UIImage imageNamed:ImageName.mine_bg_top];
        iv.contentMode = UIViewContentModeScaleAspectFill;
        iv.clipsToBounds = YES;
        _topBgImageView = iv;
    }
    return _topBgImageView;
}
- (MineNaviBar *)naviBar {
    if (!_naviBar) {
        _naviBar = [[MineNaviBar alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavBarAndStatusBarHeight)];
    }
    return _naviBar;
}
- (MineUserHead *)userHead {
    if (!_userHead) {
        _userHead = [[MineUserHead alloc]initWithFrame:CGRectMake(16, 0, kScreenWidth - 32, 60)];
    }
    return _userHead;
}
- (MineOrderIMCard *)orderImCard {
    if (!_orderImCard) {
        _orderImCard = [[MineOrderIMCard alloc]initWithFrame:CGRectMake(16, 0, kScreenWidth - 32, 158 * ADAPTER_WIDTH)];
    }
    return _orderImCard;
}
- (MineToolCard *)toolCard {
    if (!_toolCard) {
        _toolCard = [[MineToolCard alloc]initWithFrame:CGRectMake(16, 0, kScreenWidth - 32, 186)];
    }
    return _toolCard;
}
- (MineHalfCard *)halfCard {
    if (!_halfCard) {
        _halfCard = [[MineHalfCard alloc]initWithFrame:CGRectMake(16, 0, kScreenWidth - 32, 90 * ADAPTER_WIDTH)];
    }
    return _halfCard;
}
@end
